
#ifndef  BTREEPLUS_H
#define  BTREEPLUS_H

#include "common.h"


enum BtreePlusErrorCode {
    DuplicatedPrimaryKey = -101,
    KeyNotExist = -102,
};


class IBtreePlusNodeView;
class BtreePlusInteriorNodeView;
class BtreePlusPageNumberAllocator;
class Pager;
class Page;
class BtreePlus {
public:
    BtreePlus(const std::string & name); 
    ~BtreePlus();
    int open();
    int close();
    int insert(const RowIDType key, const std::string & value);
    int del(const RowIDType  key);
    int find(const RowIDType key, std::string & data) const;
    std::string toString();

    int commit();//commit
    
    static BtreePlus * Create();
    static void test();

private:
    int _insertNoFull(IBtreePlusNodeView * node, const RowIDType key, const std::string & value);
    int _split(BtreePlusInteriorNodeView * node, IBtreePlusNodeView * sub, int index);
    int _find(PageNumberType pageNo, const int64_t key, std::string & data) const;
    
    void _layerPrint(std::ostream & os);

    IBtreePlusNodeView *  getRoot() const {return root;}
    Page * allocPage();
    void freePage(Page * page);
    int writePage(Page * page);

private :
    std::string fileName;
    BtreePlusPageNumberAllocator * allocator;
    IBtreePlusNodeView * root;
    Pager * pager;
//for test
    std::deque<IBtreePlusNodeView*> lvQueue;
};

#endif   /* BTREEPLUS_H */
